package com.qiyi.components.preview.media
{
   import flash.display.Sprite;
   import com.qiyi.player.core.player.IPlayer;
   import com.qiyi.player.base.logging.ILogger;
   import flash.events.Event;
   import com.qiyi.player.core.CoreManager;
   import com.qiyi.player.core.player.def.PlayerUseTypeEnum;
   import com.qiyi.player.core.player.events.PlayerEvent;
   import com.qiyi.player.core.player.LoadMovieParams;
   import com.qiyi.components.preview.events.MediaPlayerEvent;
   import com.qiyi.player.core.video.def.StopReasonEnum;
   import com.qiyi.player.core.player.def.StatusEnum;
   import com.qiyi.player.base.logging.Log;
   
   public class PrevewVideo extends Sprite
   {
      
      public function PrevewVideo() {
         this._log = Log.getLogger("com.qiyi.components.preview.media.PrevewVideo");
         super();
         addEventListener(Event.REMOVED_FROM_STAGE,this.onRemoved);
      }
      
      private var _holder:IPlayer;
      
      private var _corePlayer:IPlayer;
      
      private var _aspect:Number;
      
      private var _videoW:Number = 0;
      
      private var _videoH:Number = 0;
      
      private var _log:ILogger;
      
      public function get aspect() : Number {
         return this._aspect;
      }
      
      override public function get width() : Number {
         return this._videoW;
      }
      
      override public function set width(param1:Number) : void {
         this._videoW = param1;
         if(this._corePlayer)
         {
            this._corePlayer.setArea(0,0,this._videoW,this._videoH);
         }
      }
      
      override public function get height() : Number {
         return this._videoH;
      }
      
      override public function set height(param1:Number) : void {
         this._videoH = param1;
         if(this._corePlayer)
         {
            this._corePlayer.setArea(0,0,this._videoW,this._videoH);
         }
      }
      
      public function setVideoSize(param1:int, param2:int) : void {
         this._videoW = param1;
         this._videoH = param2;
         if(this._corePlayer)
         {
            this._corePlayer.setArea(0,0,this._videoW,this._videoH);
         }
      }
      
      public function setModel(param1:IPlayer) : void {
         this.close();
         this._holder = param1;
         this._aspect = this._holder.movieModel.width / this._holder.movieModel.height;
      }
      
      private function onRemoved(param1:Event) : void {
         this.close();
      }
      
      public function play(param1:int) : void {
         this._log.info("preview play...");
         if(this._corePlayer == null)
         {
            this._corePlayer = CoreManager.getInstance().createPlayer(PlayerUseTypeEnum.PREVIEW);
            this._corePlayer.initialize(this,false,false);
            this._corePlayer.setArea(0,0,this._videoW,this._videoH);
            this._corePlayer.layer.showSubtitle = false;
            this._corePlayer.layer.showLogo = false;
            this._corePlayer.layer.showBrand = false;
            this._corePlayer.addEventListener(PlayerEvent.Evt_StatusChanged,this.onPlayerStatusChanged);
         }
         var _loc2_:LoadMovieParams = new LoadMovieParams();
         _loc2_.tvid = this._holder.movieModel.tvid;
         _loc2_.vid = this._holder.movieModel.vid;
         _loc2_.movieIsMember = this._holder.movieModel.member;
         _loc2_.recordHistory = false;
         _loc2_.startTime = param1;
         _loc2_.endTime = param1 + 5000;
         this._corePlayer.loadMovie(_loc2_);
      }
      
      public function close() : void {
         if(this._corePlayer)
         {
            this._corePlayer.stop();
            this._corePlayer.removeEventListener(PlayerEvent.Evt_StatusChanged,this.onPlayerStatusChanged);
            CoreManager.getInstance().deletePlayer(this._corePlayer);
            this._corePlayer = null;
         }
      }
      
      private function onPlayerStatusChanged(param1:PlayerEvent) : void {
         if(!(param1.data.isAdd as Boolean))
         {
            return;
         }
         switch(param1.data.status)
         {
            case StatusEnum.ALREADY_READY:
               this._corePlayer.setArea(0,0,this._videoW,this._videoH);
               this._corePlayer.play();
               break;
            case StatusEnum.PLAYING:
               dispatchEvent(new MediaPlayerEvent(MediaPlayerEvent.MEDIA_STATE_CHANGE,MediaState.MEDIA_FULL));
               break;
            case StatusEnum.PAUSED:
               break;
            case StatusEnum.SEEKING:
               break;
            case StatusEnum.WAITING:
               break;
            case StatusEnum.STOPPING:
               break;
            case StatusEnum.STOPED:
               if(this._corePlayer.stopReason == StopReasonEnum.REACH_ASSIGN || this._corePlayer.stopReason == StopReasonEnum.SKIP_TRAILER || this._corePlayer.stopReason == StopReasonEnum.STOP)
               {
                  this._corePlayer.replay();
               }
               break;
            case StatusEnum.FAILED:
               break;
         }
      }
   }
}
